package HashMapSeries;

import java.util.HashSet;
import java.util.Set;

public class IntersectionSolution_349 {
    public int[] intersection(int[] nums1, int[] nums2) {
        if (nums1 == null || nums2 == null || nums1.length == 0 || nums2.length == 0) {
            return new int[0];
        }

        Set<Integer> hashSet = new HashSet<>();
        Set<Integer> result = new HashSet<>();

        for (int num : nums1) {
            hashSet.add(num);
        }

        for (int num : nums2) {
            if (hashSet.contains(num)) {
                result.add(num);
            }
        }

        //转成数组返回
        int[] res = new int[result.size()];
        int j = 0;
        for (int num : result) {
            res[j++] = num;
        }

        return res;
    }
}
